package com.woniuxy.dr_mall.controller;


import com.woniuxy.dr_mall.entity.DrCategory;
import com.woniuxy.dr_mall.entity.MyResponseEntity;
import com.woniuxy.dr_mall.global.ResponseStateCode;
import com.woniuxy.dr_mall.service.IDrCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 张哲轩
 * @since 2021-08-11
 */
@RestController
@CrossOrigin
@RequestMapping("/dr_mall/drCategory")
public class DrCategoryController {

    @Autowired
    private IDrCategoryService iDrCategoryService;

    @GetMapping("getlist")
    public MyResponseEntity<List<DrCategory>> getList() {

        List<DrCategory> list = iDrCategoryService.getAll();

        MyResponseEntity<List<DrCategory>> myResponseEntity = new MyResponseEntity<>();
        myResponseEntity.setCode(ResponseStateCode.SUCCESS);
        myResponseEntity.setMsg("查询成功");
        myResponseEntity.setData(list);

        return myResponseEntity;
    }

    @PostMapping("add")
    public MyResponseEntity<String> add(@RequestBody DrCategory drCategory) {
        DrCategory dc = iDrCategoryService.getByName(drCategory.getName());
        if ("".equals(drCategory.getGuide())) {
            return MyResponseEntity.INVALIDARG;
        } else if (dc == null) {
            iDrCategoryService.addCategory(drCategory);
            return MyResponseEntity.SUCCESS;
        } else {
            return MyResponseEntity.FAILED;
        }
    }

    @GetMapping("getbyid/{id}")
    public MyResponseEntity getById(@PathVariable int id) {
        DrCategory drCategory = iDrCategoryService.getById(id);
        MyResponseEntity myResponseEntity = new MyResponseEntity();
        myResponseEntity.setCode(ResponseStateCode.SUCCESS);
        myResponseEntity.setMsg("查询成功");
        myResponseEntity.setData(drCategory);
        return myResponseEntity;
    }

    @PostMapping("update")
    public MyResponseEntity updateById(@RequestBody DrCategory drCategory) {
        iDrCategoryService.updateById(drCategory);
        if (iDrCategoryService.updateById(drCategory)) {
            return MyResponseEntity.SUCCESS;
        } else {
            return MyResponseEntity.FAILED;
        }
    }

    @GetMapping("delbyid/{id}")
    public MyResponseEntity<?> delById(@PathVariable int id) {
        iDrCategoryService.delById(id);
        return MyResponseEntity.SUCCESS;
    }

    @GetMapping("get_all_category")
    public MyResponseEntity<?> getAllCategory() {
        iDrCategoryService.getAll();
        return MyResponseEntity.SUCCESS;
    }


}

